package com.sg.service.biz.framework.nb;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.framework.req.*;
import com.sg.dto.biz.framework.res.*;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import com.wicket.okrframework.common.redis.PrivacyInfoForRedis;
import com.wicket.okrframework.common.redis.UserIndElmInfoForRedis;
import com.wicket.okrframework.common.redis.UserIndInfoForRedis;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
@BanAuto
public class NbBusinessObject {
    /**
     * code:collectionsAggregation
     * name:数据集聚合
     * desc:undefined
     **/
    @Resource
    private RedisUtil redisClient;

    @BanAuto
    @Trace(operationName = "数据集聚合")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public CollectionsAggregationRespDto collectionsAggregation(CollectionsAggregationReqDto reqDto) {
        CollectionsAggregationRespDto retData = new CollectionsAggregationRespDto();
        for (FrameworkSceneRoleDto role : reqDto.getRoleList()) {
            for (TributPowerDto power : reqDto.getTributPowerList()) {
                if (power.getAllocationObjectId().equals(role.getRoleId())) {
                    TributPowerDto retElm = new TributPowerDto();
                    BeanUtil.copyProperties(power, retElm);
                    retElm.setPositionRoleCode(role.getRoleCode());
                    retData.getTributPowerList().add(retElm);
                }
            }
        }
        return retData;
    }

    /**
     * code:putUserBasicInfoToRedis
     * name:用户基本信息存储Redis
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "用户基本信息存储Redis")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public PutUserBasicInfoToRedisRespDto putUserBasicInfoToRedis(PutUserBasicInfoToRedisReqDto reqDto) {
        PutUserBasicInfoToRedisRespDto retData = new PutUserBasicInfoToRedisRespDto();
        if (reqDto == null) {
            return retData;
        }
        Object jo = JSONArray.toJSON(reqDto);
        redisClient.set("userbase-" + reqDto.getOriginalRoleMemberId(), jo.toString());
        return retData;
    }

    /**
     * code:putUserIrInfoToRedis
     * name:用户就职信息存储Redis
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "用户就职信息存储Redis")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public PutUserIrInfoToRedisRespDto putUserIrInfoToRedis(PutUserIrInfoToRedisReqDto reqDto) {
        UserIndInfoForRedis retData = new UserIndInfoForRedis();
        retData.setUserIndList(new ArrayList<UserIndElmInfoForRedis>());
        for (OrgInductionRecordDto orgInd : reqDto.getOrgInductionRecordList()) {
            UserIndElmInfoForRedis retElm = new UserIndElmInfoForRedis();
            BeanUtil.copyProperties(orgInd, retElm);
            if (retElm.getPowerCodeList() == null) {
                retElm.setPowerCodeList(new ArrayList<PrivacyInfoForRedis>());
            }

            // 部门职务权限分配
            for (TributPowerDepTributPowerDeptPositionListtPositionDto deptPrivacyElm : reqDto
                    .getTributPowerDeptPositionList()) {
                if (deptPrivacyElm.getAllocationObjectId().equals(orgInd.getDeptPositionRelationshipId())) {
                    PrivacyInfoForRedis privacyElm = new PrivacyInfoForRedis();
                    privacyElm.setPrivacyCode(deptPrivacyElm.getPrivacyCode());
                    retElm.getPowerCodeList().add(privacyElm);
                }
            }

            // 职务权限分配
            for (TributPowerPositiontListDto positionPrivacyElm : reqDto.getTributPowerPositiontList()) {
                if (positionPrivacyElm.getAllocationObjectId().equals(orgInd.getPositionId())) {
                    PrivacyInfoForRedis privacyElm = new PrivacyInfoForRedis();
                    privacyElm.setPrivacyCode(positionPrivacyElm.getPrivacyCode());
                    retElm.getPowerCodeList().add(privacyElm);
                }
            }

            // 就职记录权限分配
            for (TributPowerInductionRecordListDto indPrivacyElm : reqDto.getTributPowerInductionRecordList()) {
                if (indPrivacyElm.getAllocationObjectId().equals(orgInd.getInductionRecordId())) {
                    PrivacyInfoForRedis privacyElm = new PrivacyInfoForRedis();
                    privacyElm.setPrivacyCode(indPrivacyElm.getPrivacyCode());
                    retElm.getPowerCodeList().add(privacyElm);
                }
            }

            // 权限分配角色
            for (TributPowerRoleListDto rolePrivacyElm : reqDto.getTributPowerRoleList()) {
                if (rolePrivacyElm.getPositionRoleCode().equals(orgInd.getPositionRoleCode())) {
                    PrivacyInfoForRedis privacyElm = new PrivacyInfoForRedis();
                    privacyElm.setPrivacyCode(rolePrivacyElm.getPrivacyCode());
                    retElm.getPowerCodeList().add(privacyElm);
                }
            }

            retData.getUserIndList().add(retElm);
        }
        Object jo = JSONArray.toJSON(retData);
        redisClient.set("inductionInfo-" + reqDto.getOriginalRoleMemberId(), jo.toString());
        return new PutUserIrInfoToRedisRespDto();
    }

    /**
     * code:calculationMethod
     * name:计算方法
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "计算方法")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public CalculationMethodRespDto calculationMethod(CalculationMethodReqDto reqDto) {
        Long lastLockoutTimeNum = reqDto.getLastLockoutTime().getTime();
        if (lastLockoutTimeNum < 0) {
            CalculationMethodRespDto tmpRetData = new CalculationMethodRespDto();
            tmpRetData.setIsInvalid("TRUE");
            return tmpRetData;
        }
        Long thresholdValueNum = Long.valueOf(reqDto.getThresholdValue());
        Long currentTime = System.currentTimeMillis() / 1000;
        Boolean boolRet = (lastLockoutTimeNum + thresholdValueNum) > currentTime;
        CalculationMethodRespDto retData = new CalculationMethodRespDto();
        if (boolRet) {
            retData.setIsInvalid("TRUE");
        } else {
            retData.setIsInvalid("FALSE");
        }
        return retData;
    }

    /**
     * code:generateToken
     * name:M-生成Token（特殊方法）
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "M-生成Token（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public GenerateTokenRespDto generateToken(GenerateTokenReqDto reqDto) {
        GenerateTokenRespDto retData = new GenerateTokenRespDto();
        retData.setToken(CommonFunctionHelper.generateToken());
        return retData;
    }
    // 手工接入方法
}
